package duoxiancheng;

/**
 * 模拟龟兔赛跑
 */
public class Guitu implements Runnable{
    private static String winner;

    @Override
    public void run() {
        for (int steps = 1; steps <=100 ; steps++) {
            System.out.println(Thread.currentThread().getName()+"---->"+steps);
            //模拟休息
            if(Thread.currentThread().getName().equals("兔子")&&steps%51==0){
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            //比赛是否结束
            boolean flag = gameOver(steps);
            if(flag){
                break;
            }
        }
    }

    private boolean gameOver(int steps){
        if(winner!=null){//存在胜利者
            return true;
        }else{
            if(steps==100){
                winner = Thread.currentThread().getName();
                System.out.println("winner--->" + winner);
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        Guitu gt = new Guitu();
        new Thread(gt,"乌龟").start();
        new Thread(gt,"兔子").start();
    }
}
